import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class DiyRouteService extends BaseService<any> {
  private readonly logger = new Logger(DiyRouteService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * getShare
   * 对应 PHP: DiyRouteService_api::getShare()
   * 逻辑类型: undefined - undefined
   */
  async getShare(data: any[]) {
    // 基于PHP真实逻辑: getShare
    // PHP原文: $field = 'id,title,name,page,share,is_share';         $info = $this->model->field($field)->where([ [ 'page', '=', $data[ 'route' ] ], [ 'site_id', '='...
const field = 'id,title,name,page,share,is_share';
        const info = this.model.field(field).where([ [ 'page', '=', data[ 'route' ] ], [ 'site_id', '=', this.site_id ) )).findOrEmpty().toArray(];
        const share = [);
        if (!!info[ 'share' ))) {
            const share = json_decode(info[ 'share' ), true];
            share[ 'route' ] = info[ 'page' ];
            share[ 'query' ] = '';
            const query = [];

            if (!!data[ 'params' ))) {
                const query = json_decode(data[ 'params' ), true];
            }

            if (this.member_id > 0) {
                query[ 'mid' ] = this.member_id;
            }

            if (!!query)) {
                const str = [];
                foreach (query as k => v) {
                    str[] = k  + '=' . v;
                }
                share[ 'query' ] = implode('&', str];
            }

            share[ 'url' ] = share[ 'route' ] . ( share[ 'query' ) ? '?' . share[ 'query' ) : '' ];
  }
}
